﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace huaguwang.hgmanage.Expert
{
    public partial class ExpertBlog_E : System.Web.UI.Page
    {

        #region 只读资料定义
        private readonly D_T_ExperInfot_Exp _DalInfo = new D_T_ExperInfot_Exp();
        private readonly D_T_ExpertBlog_Exp _Dal = new D_T_ExpertBlog_Exp();
        #endregion

        #region 固定方法
        /// <summary>
        ///  设置按钮资料
        /// </summary>
        /// <param name="i"></param>
        /// <param name="but"></param>
        private void SetBut(int i, Button but)
        {
            AdminPublic.Try(() =>
            {
                switch(i)
                {
                    case 1:
                        but.CausesValidation = false;
                        break;
                    default:
                        break;
                }
            });

        }
        /// <summary>
        /// 初始化窗口控件
        /// </summary>
        private void ControlsInit()
        {
            AdminPublic.Try(() =>
            {
                DropDownList1.DataValueField = "ID";
                DropDownList1.DataTextField = "Name";
                var id = Request.Params["InfoID"].Exp_IntTryParse();
                if(id > 0)
                {
                    DropDownList1.DataSource = _DalInfo.DropDownListDatData(id);
                }
                else
                {
                    DropDownList1.DataSource = _DalInfo.DropDownListDatData();
                }
                DropDownList1.DataBind();

            });
        }
        /// <summary>
        /// 编辑模式的时候,填充窗口
        /// </summary>
        /// <param name="model"></param>
        private void SetControlsData(M_T_ExpertBlog model)
        {
            AdminPublic.Try(() =>
            {

                txtTitle.Text = model.Title;
                txtBT.Text = model.WebTitle;
                txtBZ.Value = model.Contents;
                txtGJZ.Text = model.WebKey;
                txtLY.Text = model.Source;
                txtMS.Text = model.WebSummary;
                txtTitle.Text = model.Title;
                this.FLWUpLoad1.SetIMG(model.Pic);
                DropDownList1.SelectedValue = model.ExpertID.ToString();
                //txtTJ.Text = model.Topis.ToString();
                //CheckBox1.Checked = model.RemSign;
                if(model.RemSign)
                {
                    RB_TJ2.Checked = true;
                }
                else
                {
                    RB_TJ1.Checked = true;
                }
                if(model.Topis == 1)
                {
                    RB_PX2.Checked = true;
                }
                else
                {
                    RB_PX1.Checked = true;
                }

            });
        }
        /// <summary>
        /// 从页面控件中构造一个实体model
        /// </summary>
        /// <returns></returns>
        private M_T_ExpertBlog ControlsToModel()
        {
            var model = new M_T_ExpertBlog()
            {
                Contents = txtBZ.Value.Exp_Trim(),
                Flag = true,
                id = ViewState["ID"].Exp_IntTryParse(),
                RemSign = RB_TJ2.Checked,
                Source = txtLY.Text.Exp_Trim(),
                Title = txtTitle.Text.Exp_Trim(),
                Topis = (RB_PX2.Checked) ? 1 : 0,
                UploadDate = DateTime.Now,
                WebKey = txtGJZ.Text.Exp_Trim(),
                WebSummary = txtMS.Text.Exp_Trim(),
                WebTitle = txtBT.Text.Exp_Trim(),
                ColorKey = "",
                ColorTitle = "",
                ExpertID = DropDownList1.SelectedValue.Exp_IntTryParse(),
                InnerKey = "",
                Pic = "",
                UserID = UserConst.Exp_GetUser(this).id

            };
            return model;
        }
        /// <summary>
        /// 操作完成后,处理各种提示用
        /// </summary>
        /// <param name="iRef"></param>
        /// <param name="LogType"></param>
        public void EditSaveMsg(int iRef, Log_Type LogType)
        {
            if(iRef > 0)
            {
                this.Exp_PageWriteLog(LogType, "专家博客", iRef);
                this.Exp_PageSkyAlterReplace("ExpertBlog.aspx", WebUserToolBar1.EditSKey, "操作成功");
            }
            else
            {
                switch(iRef)
                {
                    //case 0:
                    //    this.Exp_PageAlter("操作失败,操作中出现异常");
                    //    break;
                    //case -1:
                    //    this.Exp_PageAlter("操作失败,会员资料操作失败");
                    //    break;
                    //case -2:
                    //    this.Exp_PageAlter("操作失败,权限组资料操作失败");
                    //    break;
                    default:
                        this.Exp_PageAlter("操作失败");
                        break;
                }
            }
        }
        #endregion



        protected void Page_Load(object sender, EventArgs e)
        {


            AdminPublic.Try(() =>
            {

                if(!IsPostBack)
                {
                    //初始化权限组下拉框
                    ControlsInit();
                    //存储主键
                    var id = Request.Params["ID"].Exp_IntTryParse();
                    ViewState.Add("ID", id);
                    //this.Image1.Visible = false;
                    if(id > 0)
                    {
                        var model = _Dal.GetModel(id);
                        if(model == null)
                        {
                            this.Exp_PageSkyAlterReplace("ExpertBlog.aspx", WebUserToolBar1.EditSKey, "找不到相关资料");
                        }
                        else
                        {
                            //this.Image1.Visible = true;
                            SetControlsData(model);
                        }
                    }
                }
                //工具栏初始化
                WebUserToolBar1.EditInit(
                    UserConst.Exp_GetUser,
                    UserConst.Exp_GetButtonList,
                    SetBut,
                    UserConst.Exp_ToolBarErr);


                if(!IsPostBack)
                {

                }
            });

        }

        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button0(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                //注意,统一,先获取实体,然后根据实体进行各种判断
                var model = ControlsToModel();

                var LogType = Log_Type.add;

                //if(_Dal.IsLoginName(model.Name, model.id))
                //{
                //    this.Exp_PageAlter("用户名不可以重复");
                //    return;
                //}

                var iRef = 0;
                if(0 >= model.id)
                {
                    //var fileName = this.Exp_GetSWFFile();
                    //if(fileName.Length <= 0)
                    //{
                    //    this.Exp_PageAlter("图片上传失败");
                    //    return;
                    //}
                    //else
                    //{
                    //    model.Pic = fileName;
                    //}

                    if(FLWUpLoad1.UpLoad())
                    {
                        model.Pic = FLWUpLoad1.FilePath;
                    }
                    else
                    {
                        model.Pic = string.Empty;
                    }

                    iRef = _Dal.Add(model);
                }
                else
                {
                    //    修改的专用判断
                    LogType = Log_Type.update;

                    //如果要保存图片,请注意此处的用法
                    var Temp = _Dal.GetModelFlag(model.id);
                    if(Temp == null)
                    {
                        this.Exp_PageAlter("资料不存在,可能被其他人删除");
                        return;
                    }


                    //var fileName = this.Exp_GetSWFFile();
                    //if(fileName.Length <= 0)
                    //{
                    //    //如果修改的时候没有上传图片则使用现在的
                    //    model.Pic = Temp.Pic;
                    //}
                    //else
                    //{
                    //    model.Pic = fileName;
                    //}

                    if(FLWUpLoad1.UpLoad())
                    {
                        model.Pic = FLWUpLoad1.FilePath;
                    }
                    else
                    {
                        model.Pic = Temp.Pic;
                    }


                    iRef = _Dal.Update(model);
                }
                EditSaveMsg(iRef, LogType);
            });

        }
        /// <summary>
        /// 返回
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button1(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                var sKey = WebUserToolBar1.EditSKey;
                Response.Redirect("ExpertBlog.aspx?" + sKey);
            });
        }
    }
}
